package com.codechuan.hallo.utils;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.hallo.dto.CityInfo;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;

public class WeChatUtil {
    public static void main(String[] args) {
        //System.out.println(getWeChatMiniProgramCode("15996664846"));

        //System.out.println(getWeChatMiniProgramToken("15996664846","2780"));
        CityInfo cityInfo = CommonUtil.getCityInfo("四川省德阳市旌阳区");
        cityInfo.setCityId("0838");
       // getWeChatMiniProgramOrderList(cityInfo);

    }

    /**
     * @Deprecated 获取小程序验证码
     */
    public static String getWeChatMiniProgramCode(String phone) {
        OkHttpClient client = new OkHttpClient();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("action", "user.account.sendCodeV2");
        hashMap.put("systemCode", "Aj4");
        hashMap.put("env", "pro");
        hashMap.put("mobile", phone);
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(hashMap));
        // 创建请求对象，并添加自定义的请求头
        Request request = new Request.Builder()
                .url("https://api.hellobike.com/api?user.account.sendCodeV2")
                .post(body)
                .addHeader("Host", "api.hellobike.com")
                .addHeader("Connection", "keep-alive")
                .addHeader("content-type", "application/json")
                .addHeader("Accept-Encoding", "gzip,compress,br,deflate")
                .addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x1800312d) NetType/WIFI Language/zh_CN")
                .build();
        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {

            assert response.body() != null;
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            assert jsonObject != null;
            JSONObject data = JSONObject.parseObject(jsonObject.getString("data"));
            try {
                if (data.getString("ok").equals("true")) {
                    return "发送成功";
                }
            } catch (Exception e) {
                return jsonObject.getString("msg");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "";
    }

    /**
     * @Deprecated 获取小程序Token
     */
    public static String getWeChatMiniProgramToken(String phone, String code) {
        OkHttpClient client = new OkHttpClient();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("action", "user.account.weixinMobileLogin");
        hashMap.put("platform", "1");
        hashMap.put("env", "pro");
        hashMap.put("systemCode", "Aj4");
        hashMap.put("channel", "26");
        hashMap.put("mobile", phone);
        hashMap.put("code", code);
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(hashMap));
        // 创建请求对象，并添加自定义的请求头
        Request request = new Request.Builder()
                .url("https://api.hellobike.com/api?user.account.weixinMobileLogin")
                .post(body)
                .addHeader("Host", "api.hellobike.com")
                .addHeader("Connection", "keep-alive")
                .addHeader("content-type", "application/json")
                .addHeader("Accept-Encoding", "gzip,compress,br,deflate")
                .addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x1800312d) NetType/WIFI Language/zh_CN")
                .build();
        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            assert jsonObject != null;
            String status = jsonObject.getString("code");
            if (status.equals("0")) {
                JSONObject data = JSONObject.parseObject(jsonObject.getString("data"));
                String ticket = data.getString("ticket");
                String firstLogin = data.getString("firstLogin");
                String userNewId = data.getString("userNewId");
                String newUser = data.getString("newUser");
                String mobile = data.getString("mobile");
                String guid = data.getString("guid");
                String token = data.getString("token");

                System.out.println("ticket = " + ticket);
                System.out.println("firstLogin = " + firstLogin);
                System.out.println("userNewId = " + userNewId);
                System.out.println("newUser = " + newUser);
                System.out.println("mobile = " + mobile);
                System.out.println("guid = " + guid);
                System.out.println("token = " + token);
                return "success";
            } else {
                return jsonObject.getString("msg");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static String getWeChatMiniProgramOrderList(CityInfo cityInfo) throws IOException {
        OkHttpClient client = new OkHttpClient();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("action", "hitch.driver.journeyList");
        hashMap.put("clientId", ""); // 城市id
        hashMap.put("systemCode", "62"); // 系统代码

        hashMap.put("ticket", "MTcyOTU4NTAxNw==.CuGECoMglZpdmwiNWaXcDIpnfxrhMwGq93nTG2uLzRo="); // 用户标识 ticket
        hashMap.put("token", "82eca517301d4b28917643d9276ba12b"); // 用户令牌
        hashMap.put("version", "6.63.0");

        hashMap.put("adCode", cityInfo.getAdCode()); // 城市邮编
        hashMap.put("cityCode", cityInfo.getCityId()); // 城市区号

        hashMap.put("lat", cityInfo.getLat()); //坐标 纬度
        hashMap.put("lon", cityInfo.getLng()); //坐标 经度
        hashMap.put("apkSignHash", "zkrFhoDpIPlkKlif+uTNtL1EYctTSHr7vJ/GE641gE4=\n");

        hashMap.put("bearHighwayFeeType", 0);
        hashMap.put("carpoolDeliverJointThreeOrders", false);
        hashMap.put("clickLat", "");
        hashMap.put("clickLon", "");
        hashMap.put("currentOrderGuids", new String[0]);
        hashMap.put("driverHomePageV2", false);
        hashMap.put("journeyType", 1);
        hashMap.put("journeyTypeList", new String[0]);


        hashMap.put("pageIndex", 1);//页面索引
        hashMap.put("pageSessionId", "");
        hashMap.put("pageSize", 10);
        hashMap.put("receivedPoolAgain", false);

        hashMap.put("requestSource", 0);
        hashMap.put("requestType", 3);//获取类型 3=城际大厅

        HashMap<String, Object> requestType3Param = new HashMap<>();
        requestType3Param.put("adCode", cityInfo.getAdCode());
        requestType3Param.put("cityCode", cityInfo.getCityId());
        requestType3Param.put("endAdCode", "");
        requestType3Param.put("endAdCodeList", new String[0]);
        requestType3Param.put("endCityCode", "");
        requestType3Param.put("endCityCodeList", "");
        requestType3Param.put("filters", new String[0]);
        requestType3Param.put("lat", cityInfo.getLat());
        requestType3Param.put("lon", cityInfo.getLng());
        hashMap.put("requestType3Param", requestType3Param); // 城际大厅 设置参数json

        hashMap.put("skipSourceType", 0);
        hashMap.put("sortMethod", 4);//订单排序方式 1=智能 2=最早 3=最近 4=最高
        hashMap.put("timeRange", 0);
        hashMap.put("sid", "");

        HashMap<String, Object> riskControlData = new HashMap<>();
        riskControlData.put("deviceLat", cityInfo.getLat());
        riskControlData.put("deviceLon", cityInfo.getLng());
        riskControlData.put("systemCode", "62");
        riskControlData.put("network", "wifi");
        riskControlData.put("mobileNoInfo", "");
        riskControlData.put("ssidName", "");
        riskControlData.put("capability", "WPA_PSK");
        riskControlData.put("roam", "");
        riskControlData.put("batteryLevel", "");
        riskControlData.put("isMock", 1);
        hashMap.put("riskControlData", riskControlData); //设备相关信息json

        MediaType mediaType = MediaType.parse("application/json");
        String jsonString = JSONObject.toJSONString(hashMap);

        RequestBody body = RequestBody.create(mediaType, jsonString);
        // 创建请求对象，并添加自定义的请求头
        Request request = new Request.Builder()
                .url("https://taxiapi.hellobike.com/api?hitch.driver.journeyListV2")
                .post(body)
                .build();
        // 发送请求并获取响应
        Response response = client.newCall(request).execute();
        assert response.body() != null;
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        assert jsonObject != null;
        String status = jsonObject.getString("code");
        if (status.equals("0")) {

            return jsonObject.toString();
        } else {
            return jsonObject.getString("msg");
        }
    }
}
